package com.library.management.controller;

import com.library.management.common.PageRequest;
import com.library.management.common.PageResult;
import com.library.management.common.Result;
import com.library.management.entity.BookCategory;
import com.library.management.service.BookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 图书分类控制器
 */
@Controller
@RequestMapping("/category")
public class BookCategoryController {
    @Autowired
    private BookCategoryService bookCategoryService;

    /**
     * 分类列表页面
     */
    @GetMapping("/list")
    public String list() {
        return "category/list";
    }

    /**
     * 分类详情页面
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable Long id, Model model) {
        BookCategory category = bookCategoryService.getById(id);
        model.addAttribute("category", category);
        return "category/detail";
    }

    /**
     * 分类添加页面
     */
    @GetMapping("/add")
    public String add() {
        return "category/add";
    }

    /**
     * 分类编辑页面
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        BookCategory category = bookCategoryService.getById(id);
        model.addAttribute("category", category);
        return "category/edit";
    }

    /**
     * 查询分类列表
     */
    @GetMapping("/api/list")
    @ResponseBody
    public Result<PageResult<BookCategory>> list(BookCategory category, PageRequest pageRequest) {
        PageResult<BookCategory> pageResult = bookCategoryService.list(category, pageRequest.getPageNum(),
                pageRequest.getPageSize());
        return Result.success(pageResult);
    }

    /**
     * 查询所有分类
     */
    @GetMapping("/api/listAll")
    @ResponseBody
    public Result<List<BookCategory>> listAll() {
        List<BookCategory> list = bookCategoryService.listAll();
        return Result.success(list);
    }

    /**
     * 根据ID查询分类
     */
    @GetMapping("/api/get/{id}")
    @ResponseBody
    public Result<BookCategory> get(@PathVariable Long id) {
        BookCategory category = bookCategoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 添加分类
     */
    @PostMapping("/api/add")
    @ResponseBody
    public Result<Integer> add(@RequestBody BookCategory category) {
        int result = bookCategoryService.add(category);
        return Result.success(result);
    }

    /**
     * 更新分类
     */
    @PutMapping("/api/update")
    @ResponseBody
    public Result<Integer> update(@RequestBody BookCategory category) {
        int result = bookCategoryService.update(category);
        return Result.success(result);
    }

    /**
     * 删除分类
     */
    @DeleteMapping("/api/delete/{id}")
    @ResponseBody
    public Result<Integer> delete(@PathVariable Long id) {
        int result = bookCategoryService.delete(id);
        return Result.success(result);
    }
}